var block = require('./blockChain');

var sql = require('./sql');

module.exports = {
    'POST /api/buyToken': async (ctx, next) => {
        var address = ctx.request.body.userAddress;
        var amount = ctx.request.body.amount;

        block.query.buyToken(address,amount);
        var balance = await block.query.getDenCarBalance(address);

        console.log(balance);
        // 设置Content-Type:
        ctx.response.type = 'application/json';
        // 设置Response Body:
        ctx.response.body = {
            data:balance
        };
    },
    //用户之间发送代币
    'POST /api/sendToken': async (ctx, next) => {
        var fromAddress = ctx.request.body.fromAddress;   //得到post请求的发送方地址
        var toAddress = ctx.request.body.toAddress;   //得到 接收方地址

        var amount = ctx.request.body.amount;  //得到要发送的数目

        block.query.sendToken(fromAddress,toAddress,amount);  //调用blockChain中的sendToken
        var balance = await block.query.getDenCarBalance(fromAddress); //调用blockChain中的获取余额

        console.log(balance);
        // 设置Content-Type:
        ctx.response.type = 'application/json';
        // 设置Response Body:
        ctx.response.body = {
            data:balance
        };
    },
    'POST /api/getBalance': async (ctx, next) => {

        var fromAddress = ctx.request.body.fromAddress;
        console.log(fromAddress);
        var balance = await block.query.getDenCarBalance(fromAddress);
        // 设置Content-Type:
        ctx.response.type = 'application/json';
        // 设置Response Body:
        ctx.response.body = {
            data:balance
        };
    },
    //获得下一个id
    'POST /api/getNext': async (ctx, next) => {
        var id = await sql.getNext();
        console.log('next='+id);
        // 设置Content-Type:
        ctx.response.type = 'application/json';
        // 设置Response Body:
        ctx.response.body = {
            data:id
        };
    },
    //保存评价 (fromAccount,toAccount,star,content)
    'POST /api/saveComment': async (ctx, next) => {
        var fromAddress = ctx.request.body.fromAddress;   //得到post请求的发送方地址
        var toAddress = ctx.request.body.toAddress;   //得到 接收方地址
        var star = ctx.request.body.star;  //得到评价星级
        var content = ctx.request.body.content;//得到评价内容
        sql.saveComment(fromAccount,toAccount,star,content);//保存评价
        console.log(balance);
        // 设置Content-Type:
        ctx.response.type = 'application/json';
        // 设置Response Body:
        ctx.response.body = {
            data:"success"
        };
    },
    'POST /api/testGetBalance': async (ctx, next) => {

        var fromAddress = ctx.request.body.test;
        console.log(fromAddress);
        // var balance = await block.query.getDenCarBalance(fromAddress);
        // 设置Content-Type:
        ctx.response.type = 'application/json';
        // 设置Response Body:
        ctx.response.body = {
            data:fromAddress
        };
    },
//     'POST /api/giveComment': async (ctx, next) => {
//         var fromAccount = ctx.request.body.fromAccount;
//         var toAccount = ctx.request.body.toAccount;
//         var star = ctx.request.body.star;
//         var content = ctx.request.body.content;
//
//         sql.saveComment(fromAccount,toAccount,star,content);
//
//         //TODO save in blockChain
//         // 设置Content-Type:
//         // 设置Content-Type:
//         ctx.response.type = 'application/json';
//         // 设置Response Body:
//         ctx.response.body = {
//             data:1
//         };
//     },
    //     'POST /api/giveComment': async (ctx, next) => {
//         var account = await sql.getNext();
//         account = block.query.getNextAddress(account);
//         ctx.response.type = 'application/json';
//         // 设置Response Body:
//         ctx.response.body = {
//             data: account
//         };
//     },
//
};





